import java.util.Date;

//import java.text.DateFormat;
//import java.text.SimpleDateFormat;

public class DepVal {

	public static void main(String[] args) {
		DepVal dep = new DepVal();
		long initial_time = dep.getMilliSeconds();
		// System.out.println(initial_time);

		int NUM_OF_NODES = 50;
		int NUM_OF_SENSORS = 50;
		int NUM_OF_DEPS = 50;

		NUM_OF_DEPS = Math.min(NUM_OF_NODES, NUM_OF_SENSORS);

		LOC[] Network = new LOC[NUM_OF_NODES];
		SEN[] Sensor = new SEN[NUM_OF_SENSORS];
		DEP[] Deployment = new DEP[NUM_OF_DEPS];

		double Total_Dep_Value = 0;

		for (int i = 0; i < 50; i++) {
			Sensor[i] = new SEN("Sensor " + i, 8.4, 0.8, 8, 3, 'B');
		}

//		for (int i = 300; i < 600; i++) {
//			Sensor[i] = new SEN("Sensor " + i, 8.4, 0.8, 8, 3, 'S');
//		}

//		for (int i = 600; i < 1000; i++) {
//			Sensor[i] = new SEN("Sensor " + i, 8.4, 0.8, 8, 3, 'H');
//		}

		for (int i = 0; i < 50; i++) {
			Network[i] = new LOC("Location " + i, 4, 7, 6, 'B');
		}

//		for (int i = 300; i < 600; i++) {
//			Network[i] = new LOC("Location " + i, 4, 3, 6, 'S');
//		}

//		for (int i = 600; i < 1000; i++) {
//			Network[i] = new LOC("Location " + i, 4, 7, 2, 'H');
//		}

		DEP Temp; // = new DEP(Sensor[0], Network[0]);

		boolean tMatch = false;

		for (int k = 0; k < NUM_OF_DEPS; k++) {
			// initilisation
			for (int i = 0; i < Network.length; i++) {
				for (int j = 0; j < Sensor.length; j++) {
					if (Network[i].lType == Sensor[j].sType) {
						Deployment[k] = new DEP(Sensor[j], Network[i]);
						tMatch = true;
					}
				}
			}

			// if a sensor with the same type is found
			if (tMatch == true) {

				for (int i = 0; i < Network.length; i++) {
					for (int j = 0; j < Sensor.length; j++) {
						if (Network[i].lType == Sensor[j].sType) {
							Temp = new DEP(Sensor[j], Network[i]);

							if (Temp.dDepValue > Deployment[k].dDepValue) {
								Deployment[k] = Temp;
							}// if
						}// if
					}// for
				}// for

				// Calculating total cumulative deployment value
				if (Deployment[k].dDepValue >= 1)
					Total_Dep_Value = Total_Dep_Value + Deployment[k].dDepValue;
				else
					Total_Dep_Value = Total_Dep_Value
							- (1 - Deployment[k].dDepValue);

				// print out the deployment value for each individual sensor
				// placement
				System.out.println("Deployment " + (k + 1));
				System.out.println(Deployment[k].dName);
				System.out.println("Deployment Value: "
						+ Deployment[k].dDepValue);
				if (Deployment[k].dDepValue >= 1)
					System.out.println("Deployment is effective");
				else
					System.out.println("Deployment is ineffective");

				System.out.println();

				// remove deployed sensor and location from the remaining sets
				if ((Network.length > 1) && (Sensor.length > 1)) {
					Sensor = dep.remSensor(Sensor, Deployment[k].sRef);
					Network = dep.remNode(Network, Deployment[k].nRef);
				} // end if

			} // end if

			tMatch = false;

		}// end of for loop

		System.out.println("The total deployment value for this deployment is "
				+ Total_Dep_Value);
		System.out.println();

		long final_time = dep.getMilliSeconds();
		// System.out.println(final_time);
		System.out.println("This calculation took "
				+ ((final_time - initial_time) / 1000) + " seconds");

	}// end of main

	public SEN[] remSensor(SEN[] x, SEN ref) {
		SEN[] revisedList = new SEN[x.length - 1];
		int i = 0, j = 0;

		if (x.length == 2) {
			if (x[0] == ref)
				revisedList[j] = x[1];
			else
				revisedList[j] = x[0];
		} else {
			do {
				if (x[i] == ref) {
					i++;
				}
				revisedList[j] = x[i];
				j++;
				i++;

			} while ((i < x.length) && (j < x.length - 1));
		}
		return revisedList;
	}

	public LOC[] remNode(LOC[] x, LOC ref) {
		LOC[] revisedList = new LOC[x.length - 1];
		int i = 0, j = 0;

		if (x.length == 2) {
			if (x[0] == ref)
				revisedList[j] = x[1];
			else
				revisedList[j] = x[0];
		} else {
			do {
				if (x[i] == ref) {
					i++;
				}
				revisedList[j] = x[i];
				j++;
				i++;

			} while ((i < x.length) && (j < x.length - 1));
		}
		return revisedList;
	}

	public long getMilliSeconds() {
		// DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		long x = date.getTime();
		return x; // dateFormat.format(date);

	}

} // end of class

